home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / vidmgr13.zip / VMGRDJGP.C < prev    next >
C/C++ Source or Header  |  1996-10-03  |  9KB  |  419 lines

  1. /*
  2.  *  VMGRDJGP.C; VidMgr module for the DJGPP GNU C/C++ compiler.  Release 1.2.
  3.  *
  4.  *  This module written in May 1996 by Andrew Clarke and released to the
  5.  *  public domain.
  6.  */
  7.  
  8. #include <dos.h>
  9. #include <go32.h>
  10. #include <sys/farptr.h>
  11. #include "vidmgr.h"
  12. #include "opsys.h"
  13.  
  14. static int vm_iscolorcard(void);
  15. static char vm_getscreenmode(void);
  16. static void vm_setscreenmode(char mode);
  17. static void vm_setcursorsize(char start, char end);
  18. static void vm_getcursorsize(char *start, char *end);
  19. static void vm_getkey(unsigned char *chScan, unsigned char *chChar);
  20. static unsigned long vm_screenaddress(char x, char y);
  21.  
  22. void vm_init(void)
  23. {
  24.     vm_getinfo(&vm_startup);
  25.     vm_setattr(vm_startup.attr);
  26.     opsysDetect();
  27. }
  28.  
  29. void vm_done(void)
  30. {
  31.     if (vm_getscreenmode() != vm_startup.mode)
  32.     {
  33.         vm_setscreenmode(vm_startup.mode);
  34.     }
  35.     vm_setcursorsize(vm_startup.cur_start, vm_startup.cur_end);
  36. }
  37.  
  38. void vm_getinfo(struct vm_info *v)
  39. {
  40.     v->ypos = vm_wherey();
  41.     v->xpos = vm_wherex();
  42.     v->attr = vm_getattrxy(v->xpos, v->ypos);
  43.     v->mode = vm_getscreenmode();
  44.     v->height = vm_getscreenheight();
  45.     v->width = vm_getscreenwidth();
  46.     vm_getcursorsize(&v->cur_start, &v->cur_end);
  47. }
  48.  
  49. static int vm_iscolorcard(void)
  50. {
  51.     return vm_getscreenmode() != 7;
  52. }
  53.  
  54. static char vm_getscreenmode(void)
  55. {
  56.     return (char)_farpeekb(_go32_conventional_mem_selector(), 0x0449);
  57. }
  58.  
  59. static void vm_setscreenmode(char mode)
  60. {
  61.     union REGS regs;
  62.     regs.h.ah = 0x00;
  63.     regs.h.al = mode;
  64.     int86(0x10, ®s, ®s);
  65. }
  66.  
  67. char vm_getscreenwidth(void)
  68. {
  69.     return (char)_farpeekb(_go32_conventional_mem_selector(), 0x044a);
  70. }
  71.  
  72. char vm_getscreenheight(void)
  73. {
  74.     return (char)(_farpeekb(_go32_conventional_mem_selector(), 0x0484) + 1);
  75. }
  76.  
  77. short vm_getscreensize(void)
  78. {
  79.     return (short)_farpeekw(_go32_conventional_mem_selector(), 0x044c);
  80. }
  81.  
  82. void vm_gotoxy(char x, char y)
  83. {
  84.     union REGS regs;
  85.     regs.h.ah = 0x02;
  86.     regs.h.bh = 0;
  87.     regs.h.dh = (unsigned char)(y - 1);
  88.     regs.h.dl = (unsigned char)(x - 1);
  89.     int86(0x10, ®s, ®s);
  90. }
  91.  
  92. char vm_wherex(void)
  93. {
  94.     return (char)(_farpeekb(_go32_conventional_mem_selector(), 0x0450) + 1);
  95. }
  96.  
  97. char vm_wherey(void)
  98. {
  99.     return (char)(_farpeekb(_go32_conventional_mem_selector(), 0x0451) + 1);
  100. }
  101.  
  102. static void vm_setcursorsize(char start, char end)
  103. {
  104.     union REGS regs;
  105.     regs.h.ah = 0x01;
  106.     regs.h.ch = start;
  107.     regs.h.cl = end;
  108.     int86(0x10, ®s, ®s);
  109. }
  110.  
  111. static void vm_getcursorsize(char *start, char *end)
  112. {
  113.     union REGS regs;
  114.     regs.h.ah = 0x03;
  115.     regs.h.bh = 0;
  116.     int86(0x10, ®s, ®s);
  117.     *start = regs.h.ch;
  118.     *end = regs.h.cl;
  119. }
  120.  
  121. int vm_kbhit(void)
  122. {
  123.     union REGS regs;
  124.     static unsigned short counter = 0;
  125.     if (counter % 10 == 0)
  126.     {
  127.         opsysTimeSlice();
  128.     }
  129.     counter++;
  130.     regs.h.ah = 0x01;
  131.     int86(0x16, ®s, ®s);
  132.     return !(regs.x.flags & 0x40);
  133. }
  134.  
  135. static void vm_getkey(unsigned char *chScan, unsigned char *chChar)
  136. {
  137.     union REGS regs;
  138.     regs.h.ah = 0x00;
  139.     int86(0x16, ®s, ®s);
  140.     *chScan = regs.h.ah;
  141.     *chChar = regs.h.al;
  142. }
  143.  
  144. int vm_getch(void)
  145. {
  146.     unsigned char chChar, chScan;
  147.  
  148.     while (!vm_kbhit())
  149.     {
  150.         /* nada */
  151.     }
  152.  
  153.     vm_getkey(&chScan, &chChar);
  154.     if (chChar == 0xe0)
  155.     {
  156.         if (chScan)
  157.         {
  158.             chChar = 0;         /* force scan return */
  159.         }
  160.         else
  161.         {                       /* get next block */
  162.             chChar = 0;
  163.  
  164.             vm_getkey(&chScan, &chChar);
  165.             if (!chScan)
  166.             {                   /* still no scan? */
  167.                 chScan = chChar;  /* move new char over */
  168.                 chChar = 0;     /* force its return */
  169.             }
  170.             else
  171.             {
  172.                 chChar = 0;     /* force new scan */
  173.             }
  174.         }
  175.     }
  176.     if (chScan == 0xe0)
  177.     {
  178.         if (!chChar)
  179.         {
  180.             chScan = 0;
  181.  
  182.             vm_getkey(&chScan, &chChar);
  183.             if (!chScan)
  184.             {                   /* still no scan? */
  185.                 chScan = chChar;  /* move new char over */
  186.                 chChar = 0;     /* force its return */
  187.             }
  188.             else
  189.             {
  190.                 chChar = 0;     /* force new scan */
  191.             }
  192.         }
  193.         else
  194.         {
  195.             chScan = 0;         /* handle 0xe00d case */
  196.         }
  197.     }
  198.     if (chChar)
  199.     {
  200.         chScan = 0;
  201.     }
  202.  
  203.     return (int)((chScan << 8) + (chChar));
  204. }
  205.  
  206. void vm_setcursorstyle(int style)
  207. {
  208.     if (vm_iscolorcard())
  209.     {
  210.         switch (style)
  211.         {
  212.         case CURSORHALF:
  213.             vm_setcursorsize(4, 7);
  214.             break;
  215.         case CURSORFULL:
  216.             vm_setcursorsize(0, 7);
  217.             break;
  218.         case CURSORNORM:
  219.             vm_setcursorsize(7, 8);
  220.             break;
  221.         case CURSORHIDE:
  222.             vm_setcursorsize(32, 32);
  223.             break;
  224.         default:
  225.             break;
  226.         }
  227.     }
  228.     else
  229.     {
  230.         switch (style)
  231.         {
  232.         case CURSORHALF:
  233.             vm_setcursorsize(8, 13);
  234.             break;
  235.         case CURSORFULL:
  236.             vm_setcursorsize(0, 13);
  237.             break;
  238.         case CURSORNORM:
  239.             vm_setcursorsize(11, 13);
  240.             break;
  241.         case CURSORHIDE:
  242.             vm_setcursorsize(32, 32);
  243.             break;
  244.         default:
  245.             break;
  246.         }
  247.     }
  248. }
  249.  
  250. static unsigned long vm_screenaddress(char x, char y)
  251. {
  252.     unsigned short base;
  253.     if (vm_iscolorcard())
  254.     {
  255.         base = opsysGetVideoSeg(0xB800);
  256.     }
  257.     else
  258.     {
  259.         base = opsysGetVideoSeg(0xB000);
  260.     }
  261.     return (unsigned long)((base << 4) + ((y - 1) * vm_getscreenwidth() * 2) + ((x - 1) * 2));
  262. }
  263.  
  264. char vm_getchxy(char x, char y)
  265. {
  266.     unsigned long address;
  267.     address = vm_screenaddress(x, y);
  268.     return (char)_farpeekb(_go32_conventional_mem_selector(), address);
  269. }
  270.  
  271. char vm_getattrxy(char x, char y)
  272. {
  273.     unsigned long address;
  274.     address = vm_screenaddress(x, y);
  275.     return (char)_farpeekb(_go32_conventional_mem_selector(), address + 1L);
  276. }
  277.  
  278. void vm_xgetchxy(char x, char y, char *attr, char *ch)
  279. {
  280.     unsigned long address;
  281.     address = vm_screenaddress(x, y);
  282.     *ch = (char)_farpeekb(_go32_conventional_mem_selector(), address);
  283.     *attr = (char)_farpeekb(_go32_conventional_mem_selector(), address + 1L);
  284. }
  285.  
  286. void vm_putch(char x, char y, char ch)
  287. {
  288.     unsigned long address;
  289.     address = vm_screenaddress(x, y);
  290.     _farpokeb(_go32_conventional_mem_selector(), address, ch);
  291. }
  292.  
  293. void vm_puts(char x, char y, char *str)
  294. {
  295.     char ofs;
  296.     ofs = 0;
  297.     while (*str)
  298.     {
  299.         unsigned long address;
  300.         address = vm_screenaddress(x + ofs, y);
  301.         _farpokeb(_go32_conventional_mem_selector(), address, *str);
  302.         str++;
  303.         ofs++;
  304.     }
  305. }
  306.  
  307. void vm_xputch(char x, char y, char attr, char ch)
  308. {
  309.     unsigned long address;
  310.     address = vm_screenaddress(x, y);
  311.     _farpokeb(_go32_conventional_mem_selector(), address, ch);
  312.     _farpokeb(_go32_conventional_mem_selector(), address + 1L, attr);
  313. }
  314.  
  315. void vm_xputs(char x, char y, char attr, char *str)
  316. {
  317.     char ofs;
  318.     ofs = 0;
  319.     while (*str)
  320.     {
  321.         unsigned long address;
  322.         address = vm_screenaddress(x + ofs, y);
  323.         _farnspokeb(address, *str);
  324.         _farnspokeb(address + 1L, attr);
  325.         str++;
  326.         ofs++;
  327.     }
  328. }
  329.  
  330. void vm_putattr(char x, char y, char attr)
  331. {
  332.     unsigned long address;
  333.     address = vm_screenaddress(x, y);
  334.     _farpokeb(_go32_conventional_mem_selector(), address + 1L, attr);
  335. }
  336.  
  337. void vm_paintclearbox(char x1, char y1, char x2, char y2, char attr)
  338. {
  339.     char x, y;
  340.     for (y = y1; y <= y2; y++)
  341.     {
  342.         for (x = x1; x <= x2; x++)
  343.         {
  344.             vm_xputch(x, y, attr, ' ');
  345.         }
  346.     }
  347. }
  348.  
  349. void vm_paintbox(char x1, char y1, char x2, char y2, char attr)
  350. {
  351.     char x, y;
  352.     for (y = y1; y <= y2; y++)
  353.     {
  354.         for (x = x1; x <= x2; x++)
  355.         {
  356.             vm_putattr(x, y, attr);
  357.         }
  358.     }
  359. }
  360.  
  361. void vm_clearbox(char x1, char y1, char x2, char y2)
  362. {
  363.     char x, y;
  364.     for (y = y1; y <= y2; y++)
  365.     {
  366.         for (x = x1; x <= x2; x++)
  367.         {
  368.             vm_putch(x, y, ' ');
  369.         }
  370.     }
  371. }
  372.  
  373. void vm_fillbox(char x1, char y1, char x2, char y2, char ch)
  374. {
  375.     char x, y;
  376.     for (y = y1; y <= y2; y++)
  377.     {
  378.         for (x = x1; x <= x2; x++)
  379.         {
  380.             vm_putch(x, y, ch);
  381.         }
  382.     }
  383. }
  384.  
  385. void vm_gettext(char x1, char y1, char x2, char y2, char *dest)
  386. {
  387.     char x, y;
  388.     for (y = y1; y <= y2; y++)
  389.     {
  390.         for (x = x1; x <= x2; x++)
  391.         {
  392.             vm_xgetchxy(x, y, dest + 1, dest);
  393.             dest += 2;
  394.         }
  395.     }
  396. }
  397.  
  398. void vm_puttext(char x1, char y1, char x2, char y2, char *srce)
  399. {
  400.     char x, y;
  401.     for (y = y1; y <= y2; y++)
  402.     {
  403.         for (x = x1; x <= x2; x++)
  404.         {
  405.             vm_xputch(x, y, *(srce + 1), *srce);
  406.             srce += 2;
  407.         }
  408.     }
  409. }
  410.  
  411. void vm_horizline(char x1, char x2, char row, char attr, char ch)
  412. {
  413.     char x;
  414.     for (x = x1; x <= x2; x++)
  415.     {
  416.         vm_xputch(x, row, attr, ch);
  417.     }
  418. }
  419.